package com.realshare.frame.system.web.system;

import com.realshare.frame.system.web.BaseController;
import com.realshare.frame.system.model.PageResult;
import com.realshare.frame.system.model.SysResources;
import com.realshare.frame.system.model.User;
import com.realshare.frame.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * ${DESCRIPTION}
 *
 * @author realshare
 * @email 15075107780@139.com
 * @create 2018-07-26 23:49
 * @modify 2018-07-26 23:49
 **/
@RestController
@RequestMapping("/res")
public class ResourcesController extends BaseController {

    //简单的后台接口，用于测试
    @RequestMapping("/info")
    public Object info(User user) {
        sqlManager.single(User.class, user.getRealName());
        List list = sqlManager.query(User.class).andEq("realName", "11").orderBy("userName").select();
        //声明一个sql存放的对象
        //SQLReady sqlReady = new SQLReady("insert into user set id='"+ UUID.randomUUID()+"',user_name=1,real_name=1");
        //若是执行持久化，则是
        //sqlManager.executeUpdate(sqlReady);
        sqlManager.update("user.updateUserById");
        //sqlManager.update("user.updateUser");
        return user;
    }

    @RequestMapping("/deleteResources")
    public PageResult deleteResources(SysResources sysResources) {
        sqlManager.updateTemplateById(sysResources);
        pageResult.setMessage("删除成功！");
        return pageResult;
    }

    @RequestMapping("/saveResources")
    public PageResult saveResources(SysResources sysResources) {
        if (StringUtils.isBlank(sysResources.getId())) {
            sqlManager.insert(sysResources);
        } else {
            sqlManager.updateById(sysResources);
        }
        pageResult.setMessage("保存成功！");
        return pageResult;
    }

    @RequestMapping("/getNodesByPid")
    public PageResult getNodesByPid(String parentId) {
        parentId = StringUtils.isBlank(parentId) ? "0" : parentId;
        List<SysResources> resources = sqlManager.query(SysResources.class)
                .andEq("status", "1")
                .andEq("parent_id", parentId)
                //.desc("create_time")
                .select();
        pageResult.setRows(resources);
        return pageResult;
    }
}
